#include "App_Communication.h"
// Delete Modify in gitee
// Modify in gitbash

void App_Communication_Init(void){
    Inf_AT6558R_Init();
    Inf_QS100_Init();
    Inf_Lora_Init();
}

uint8_t location[800] = "$GNGGA,013914.000,2236.89686,N,11407.67363,E,1,10,1.7,143.3,M,-2.9,M,,*50$GNGLL,2236.89686,N,11407.67363,E,013914.000,A,A*41$GNGSA,A,3,18,23,28,,,,,,,,,,3.5,1.7,3.0,1*30$GNGSA,A,3,06,09,14,16,24,25,38,,,,,,3.5,1.7,3.0,4*30$GPGSV,2,1,07,10,58,336,19,12,34,077,,15,07,089,,18,20,185,13,0*60$GPGSV,2,2,07,23,71,115,38,28,38,242,31,32,36,316,,0*5C$BDGSV,2,1,08,03,,,34,06,70,281,22,09,61,256,30,14,59,034,29,0*48$BDGSV,2,2,08,16,72,300,27,24,71,074,24,25,45,204,35,38,75,137,36,0*72$GNRMC,013914.000,A,2236.89686,N,11407.67363,E,1.21,330.64,281124,,,A,V*00$GNVTG,330.64,T,,M,1.21,N,2.25,K,A*26$GNZDA,013914.000,28,11,2024,00,00*48$GPTXT,01,01,01,ANTENNA OPEN*25";
uint16_t locationMsgSize = 20;

//uint8_t location[800] ;
//uint16_t locationMsgSize ;

/**
 * @brief 将UTC时间转成北京时间
 * 
 * @param data 
 */
static void App_Communication_UTCTime2BeiJingTime(UploadData* data){
    //TODO 补全代码
    struct tm utc;
    // debug_println("datetime:%s", data->dateTime);
    sscanf((char*)data->dateTime, "%d-%d-%d %d:%d:%d", &utc.tm_year, &utc.tm_mon, &utc.tm_mday, &utc.tm_hour, &utc.tm_min, &utc.tm_sec);
    // debug_println("%04d-%02d-%02d %02d:%02d:%02d", utc.tm_year, utc.tm_mon, utc.tm_mday, utc.tm_hour, utc.tm_min, utc.tm_sec);

    utc.tm_year -= 1900;
    utc.tm_mon -= 1;
    time_t utc_sec = mktime(&utc);
    time_t beijing_sec = utc_sec + 8 * 3600;
    struct tm* beijing_time;
    beijing_time = localtime(&beijing_sec);
    // debug_println("");
    sprintf((char*)data->dateTime, "%04d-%02d-%02d %02d:%02d:%02d", beijing_time->tm_year + 1900, beijing_time->tm_mon + 1, beijing_time->tm_mday, beijing_time->tm_hour, beijing_time->tm_min, beijing_time->tm_sec);
    // debug_println("datetime:%s", data->dateTime);
}

/**
 * @brief 根据结构体生成Json字符串
 * 
 * @param data 
 */
static void App_Communication_JsonFormat(UploadData* data){
    //TODO 补全代码
    data->lat = ((int)data->lat / 100) + (data->lat - (((int)data->lat / 100) * 100)) / 60;
    data->lon = ((int)data->lon / 100) + (data->lon - (((int)data->lon / 100) * 100)) / 60;
    
    cJSON *monitor = cJSON_CreateObject();

    uint8_t UID[40] = {0};
    sprintf((char*)UID, "%08X%08X%08X", HAL_GetUIDw2(), HAL_GetUIDw1(), HAL_GetUIDw0());
    cJSON_AddStringToObject(monitor, "datetime", data->dateTime);
    cJSON_AddStringToObject(monitor, "UID", UID);
    cJSON_AddNumberToObject(monitor, "lat", data->lat);
    cJSON_AddStringToObject(monitor, "latDir", (char*)&data->latDir);
    cJSON_AddNumberToObject(monitor, "lon", data->lon);
    cJSON_AddStringToObject(monitor, "lonDir", (char*)&data->lonDir);
    cJSON_AddNumberToObject(monitor, "spd", data->spd);
    cJSON_AddNumberToObject(monitor, "cog", data->cog);
    char* string = cJSON_PrintUnformatted(monitor);
    data->jsonLen = strlen(string);
    memcpy(data->jsonData, string, data->jsonLen);
    cJSON_Delete(monitor);
    debug_println("cJSON_String:%s", data->jsonData);
}
/**
 * @brief 获取定位数据
 * 
 * @param data 
 */
void App_Communication_GetLocation(UploadData* data){
    //TODO 补全代码
    //1、获取GSP数据，已存在location数组中，直接解析
    //$GNRMC,013914.000,A,2236.89686,N,11407.67363,E,1.21,330.64,281124,,,A,V
    char* RMC = NULL;
    RMC = strstr((char*)location, "RMC"); //定位到RMC地址
    char Date[10] = {0}, Time[10] = {0}, status = 0;
    debug_println("RMC:%s", RMC);
    sscanf((char*)RMC, "%*[^,],%*[^,],%c", &status);
    // debug_println("status:%c",status);
    if(status == 'A')
    {
        // debug_println("-----数据有效-----");
        // sscanf((char*)RMC, "%*[^,],%[^.]%*[^,],A,%lf,%c,%lf,%c,%lf,%lf,%[^,]", Time, &data->lat, &data->latDir, &data->lon, &data->latDir, &data->spd, &data->cog, Date);
        sscanf((char*)RMC, "RMC,%[^.].%*[^,],A,%lf,%c,%lf,%hc,%lf,%lf,%[^,]",Time, &data->lat, &data->latDir, &data->lon, &data->lonDir, &data->spd, &data->cog, Date);
        // debug_println("Time:%s -- status:%c -- lat:%lf -- ulat:%c -- lon:%lf -- ulon:%c -- spd:%lf -- cog:%lf -- Date:%s", Time, status, data->lat, data->latDir, data->lon, data->lonDir, data->spd, data->cog, Date);
        sprintf((char*)data->dateTime, "20%c%c-%c%c-%c%c %c%c:%c%c:%c%c", Date[4], Date[5], Date[2], Date[3], Date[0], Date[1], Time[0], Time[1], Time[2], Time[3], Time[4], Time[5], Time[6]);
        App_Communication_UTCTime2BeiJingTime(data);
        App_Communication_JsonFormat(data);
    }
}
/**
 * @brief 上传数据到云端服务器
 * 
 * @param data 
 */
void App_Communication_TransmitData(UploadData* data){

    CommonStatus status = Inf_QS100_TcpSendData(data->jsonData,data->jsonLen);
    if(status == COMMON_ERROR){
        Inf_Lora_Transmit(data->jsonData,data->jsonLen);
    }
}
